#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void ReverseWord(char* start, char* end)
{
	char *p = start;
	char *q = end;
	
	while(q > p)
	{
		if (*p != *q)
		{
			*p = *q ^ *p;
			*q = *q ^ *p;
			*p = *q ^ *p;
		}
		
		p ++;
		q --;
	}

}


// use xor to change
void Reverse2(char* s)
{
	char *p = s;
	char *q = s;

	while (*q != '\0'){
		q ++;
	} 
	q --;

	while(q > p)
	{
		if (*p != *q)
		{
			*p = *q ^ *p;
			*q = *q ^ *p;
			*p = *q ^ *p;
		}
		
		p ++;
		q --;
	}
	
}


// use '\0' change
void Reverse(char* s){
	char *p = s;
	char *r = s;
	char *q = s;

	while (*r != '\0'){
		r ++;
	} 
	q = r;
	q --;

	while(q > p)
	{
		*r = *p;
		*p = *q;
		*q = *r;
		p ++;
		q --;
	}
	*r = '\0';
}

void reverseStringNotWord(char*s){
	char *p = s;
	char *q = s;

	while (*q!='\0')
	{
		// ignore the first space
		
		// find the right start of the word
		while(*p==' ')
		    	p++;
		
		q = q++;

		if (*q == ' ' && *(q-1)!=' ')
		{
			ReverseWord(p,q-1);
			p = q;
		    
		}
	}
	// the last one
	if (*p != '\0')
		ReverseWord(p,q-1);
	Reverse2(s);
	
}

int ReversePrint(char* s){
	char *p = s;

	while(*p != '\0')
		p++;
	p--;
	while(p>=s)
	{
		printf("%c", *p);
		p--;
	}
	printf("\n");
	return 0;
}

int main(int argc, char const *argv[])
{
	char str[] = "sentence a is This";
	ReversePrint(str);
	//Reverse2(str);
	printf("%s\n", str);
	reverseStringNotWord(str);
	printf("%s\n", str);
	return 0;
}